<template>
  <view class="u-page">
    <view class="u-demo-block">
      <view class="u-demo-block__content">
        <u--form labelWidth="100px" labelPosition="left" :model="form" :rules="rules" ref="form1">
          <u-form-item label="姓名" borderBottom ref="item1"><u--input v-model="form.jobSeekerBySelfDto.realName" border="none"></u--input></u-form-item>

          <u-form-item label="电话" borderBottom ref="item1">
            <u--input v-model="form.jobSeekerBySelfDto.telPhone" type="number" border="none" trim maxlength="11"></u--input>
          </u-form-item>

          <u-form-item label="身份证号" borderBottom ref="item1">
            <u--input v-model="form.jobSeekerBySelfDto.idCard" type="idcard" border="none" trim maxlength="18"></u--input>
          </u-form-item>

          <u-form-item label="面试日期" borderBottom @click="showBirthday = true" ref="item1">
            <u--input v-model="form.interviewTime" disabled disabledColor="#ffffff" border="none"></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="岗位类别" borderBottom @click="showSex = true" ref="item1">
            <u--input v-model="form.jobTypeName" disabledColor="#ffffff" border="none" disabled></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="岗位编号" borderBottom @click="showCode = true" ref="item">
            <u--input v-model="form.myjobNum" disabledColor="#ffffff" border="none" disabled></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="使用推荐码" borderBottom>
            <u-checkbox-group iconPlacement="right" v-model="checkArr" @change="checkboxChange" placement="column">
              <u-checkbox :checked="checkNum === 1 ? 'true' : 'false'" shape="circle" size="12"></u-checkbox>
            </u-checkbox-group>
          </u-form-item>

          <u-form-item v-if="whetherShow == true" label="推荐码" borderBottom><u--input v-model="codeObj.recommendedCode" border="none" trim></u--input></u-form-item>

          <u-form-item v-if="whetherShow == true" label="推荐人姓名" borderBottom>
            <u--input disabled disabledColor="#fff" v-model="codeObj.realName" border="none" trim></u--input>
          </u-form-item>
        </u--form>
        <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>

        <u-action-sheet :show="showSex" :actions="actions" title="请选择" @close="showSex = false" @select="sexSelect"></u-action-sheet>

        <u-action-sheet
          style="height: 500px !important; overflow: auto !important;"
          :show="showCode"
          :actions="actionsList"
          safeAreaInsetBottom
          title="请选择"
          @close="showCode = false"
          @select="codeSelect"
        ></u-action-sheet>

        <u-datetime-picker
          :show="showBirthday"
          :value="birthday"
          mode="date"
          closeOnClickOverly
          @confirm="birthdayConfirm"
          @cancel="birthdayClose"
          @close="birthdayClose"
        ></u-datetime-picker>
      </view>
    </view>

    <!-- 更新个人信息的弹窗 -->
    <view v-if="show" class="mymessage" style="background-color: #FFFFFF;">
      <view class="showheader">提示</view>
      <view class="showtext">是否保存当前登记信息至个人中心</view>
      <view class="showbtns">
        <view class="btns" style="background-color: #02a7f0;" @click="getchange">确定</view>
        <view class="btns" style="background-color: #6fb83e;" @click="dongetchange">取消</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'formO',
  props: {
    // position: {
    //   type: [Object, Array, String]
    // },
    // userData: {
    //   type: Object
    // }
  },
  data() {
    return {
      value: '',
      showBirthday: false,
      show: false,
      codeObj: {
        recommendedCode: '',
        realName: ''
      },
      checkArr: [],
      form: {
        interviewTime: '', //面试日期
        isUpdate: 1, //是否更新
        jobNum: '', //职位代号
        jobTypeId: null, //职位类型id
        recruitmentType: 1, //社招
        signUpType: 3, //自荐
        recommendedCode: '', //推荐码
        jobSeekerBySelfDto: {
          avatarUrl: '', //头像地址
          graduationTime: '', //毕业时间
          openId: '',
          realName: '', //姓名
          telPhone: '', //电话
          idCard: '', //身份证
          workNum: '', //工号
          schoolProveUrl: '' //学历证明
        }
      },
      userData: {},
      // exchangeCode:'',
      id: null,
      position: [],
      showSex: false,
      showCode: false,
      checked: true,
      birthday: Number(new Date()),
      actions: [],
      actionsList: [],

      checkNum: null, //复选款选中的zhi
      whetherShow: true,
      // 校验规则
      rules: {
        gangwei: {
          type: 'string',
          required: true,
          message: '请填写岗位类别',
          trigger: ['blur', 'change']
        },
        code: {
          type: 'string',
          required: true,
          message: '请填写岗位编号',
          trigger: ['blur', 'change']
        },
        'form.interviewTime': {
          type: 'string',
          required: true,
          message: '请选择日期',
          trigger: ['change']
        }
      }
    };
  },
  created() {
    this.userupdate();
    this.getposition();
    this.getshuju();
    this.userData = uni.getStorageSync('userinfo');
  },
  mounted() {
    this.userupdate();

    const arr = [];

    for (var i = 0; i < this.position.length; i++) {
      arr.push({
        name: this.position[i].typeName,
        id: this.position[i].id,
        keyWord: this.position[i].keyWord
      });
    }
    this.actions = arr;

    this.checkNum = 1;
  },
  methods: {
    // 获取岗位
    async getposition() {
      console.log(12311223);
      const { data } = await uni.$http.get('/jobType/findAll');
      if (data.statusCode !== 200) {
        return;
      } else {
        this.position = data.data;
        console.log('gggggg', this.position);
      }
    },
    // 获取分享人信息
    async getshuju() {
      console.log('uni.getStorageSync', uni.getStorageSync('scene'));
      const str = uni.getStorageSync('scene');
      console.log('我是哦therOPENid', str);

      const { data } = await uni.$http.get('/jobSeeker/findJobSeekerDetailByOpenId?scene=' + str + '&openId=' + uni.getStorageSync('openid'));
      if (data.statusCode !== 200) {
        return;
      } else {
        if (data.data === null) {
          return (this.checked = false);
        } else {
          this.checked = true;
          this.checkNum = 0;
          this.codeObj.recommendedCode = data.data.recommendedCode;
          this.codeObj.realName = data.data.realName;
        }
      }
    },
    // 复选框是否选中
    checkboxChange(e) {
      this.checkNum = e.length;
      if (this.checkNum === 1) {
        this.whetherShow = true;
      } else {
        this.whetherShow = false;
      }

      console.log('this.checkNum===>', this.checkNum);
    },
    userupdate() {
      this.form.jobSeekerBySelfDto.realName = this.userData.realName;
      this.form.jobSeekerBySelfDto.graduationTime = this.userData.graduationTime;
      this.form.jobSeekerBySelfDto.openId = this.userData.openId;
      this.form.jobSeekerBySelfDto.avatarUrl = this.userData.avatarUrl;
      this.form.jobSeekerBySelfDto.telPhone = this.userData.telPhone;
      this.form.jobSeekerBySelfDto.idCard = this.userData.idCard;
      this.form.jobSeekerBySelfDto.schoolProveUrl = this.userData.schoolProveUrl;
      this.form.jobSeekerBySelfDto.workNum = this.userData.workNum;
    },
    async sexSelect(e) {
      console.log(e);
      this.form.myjobNum = '';
      this.form.jobTypeName = e.name;
      this.form.jobTypeId = e.id;
      this.$refs.form1.validateField('gangwei');
      const { data } = await uni.$http.get('/job/findJobNumByTypeId?id=' + e.id);

      if (data.statusCode !== 200) {
        return;
      } else {
        const obj = data.data;
        const arr = [];
        console.log('我是obj', obj);
        for (var i = 0; i < obj.length; i++) {
          console.log('iiiii', obj[i]);
          arr.push({
            name: e.keyWord + obj[i]
          });
        }
        this.actionsList = arr;
        console.log(this.$refs.item);
      }
    },
    codeSelect(e) {
      console.log('eeeeeeeeeeeeeee', e);
      this.form.jobNum = e.name;
      this.form.myjobNum = e.name;
      this.$refs.form1.validateField('code');
    },
    change(e) {},
    birthdayClose() {
      this.showBirthday = false;
      this.$refs.form1.validateField('form.interviewTime');
    },
    birthdayConfirm(e) {
      this.showBirthday = false;
      this.form.interviewTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
      this.$refs.form1.validateField('form.interviewTime');
    },
    open() {
      // console.log('open');
    },
    close() {
      this.show = false;
      // console.log('close');
    },

    tujiao() {
      uni.request({
        url: uni.$http.baseUrl + '/sign/signUpAddBySelf',
        method: 'post',
        data: this.form,
        header: {
          token: uni.getStorageSync('tokenValue')
        },
        success(res) {
          if (res.data.statusCode !== 200) {
            uni.showToast({
              title: res.data.message,
              icon: 'none'
            });
          } else {
            uni.showToast({
              title: res.data.message,
              icon: 'success'
            });
            setTimeout(function() {
              uni.navigateBack();
            }, 700);
          }
        }
      });
    },
    // 提交事件
    submit() {
      // 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
      this.$refs.form1
        .validate()
        .then(res => {
          // uni.$u.toast('校验通过')
          console.log('this.form111', this.form);
          console.log('this.form999', this.form.jobNum);

          this.form.jobNum = this.form.jobNum.replace(/\D/g, '');
          console.log('this.form99', this.form.jobNum);
          const obj = uni.getStorageSync('userinfo');
          const objone = {
            avatarUrl: obj.avatarUrl, //头像地址
            graduationTime: obj.graduationTime, //毕业时间
            openId: obj.openId,
            realName: obj.realName, //姓名
            telPhone: obj.telPhone, //电话
            idCard: obj.idCard, //身份证
            workNum: obj.workNum, //工号
            schoolProveUrl: obj.schoolProveUrl //学历证明
          };
          if (JSON.stringify(this.form.jobSeekerBySelfDto) === JSON.stringify(objone)) {
            // console.log('this.numberShow == 0' , this.checkNum !== 0)
            console.log('this.checkNum============>', this.checkNum);
            if (this.checkNum === 1) {
              this.form.recommendedCode = this.codeObj.recommendedCode;
              this.tujiao();
            } else {
              this.form.recommendedCode = '';
              this.tujiao();
            }
          } else {
            this.show = true;
          }
        })
        .catch(errors => {
          uni.$u.toast('校验失败');
          console.log('errors', errors);
        });
    },
    // 更新用户信息并提交
    async getchange() {
      this.form.isUpdate = 1;
      this.show = false;
      if (this.checkNum === 1) {
        this.form.recommendedCode = this.codeObj.recommendedCode;
      } else {
        this.form.recommendedCode = '';
      }
      const res = await uni.$http.post('/sign/signUpAddBySelf', this.form);

      console.log('提交提交', this.form);
      console.log('提交提交', this.form.jobSeekerBySelfDto);
      console.log('res', res);
      if (res.data.statusCode !== 200) {
        uni.showToast({
          title: res.data.message,
          icon: 'none'
        });
      } else {
        uni.setStorageSync('userinfo', this.form.jobSeekerBySelfDto);
        uni.showToast({
          title: res.data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    },
    // 不更新用户信息并提交
    async dongetchange() {
      this.form.isUpdate = 0;
      this.show = false;

      if (this.checkNum === 1) {
        this.form.recommendedCode = this.codeObj.recommendedCode;
      } else {
        this.form.recommendedCode = '';
      }
      const res = await uni.$http.post('/sign/signUpAddBySelf', this.form);
      console.log('提交提交', this.form);
      console.log('res', res);
      if (res.data.statusCode !== 200) {
        uni.showToast({
          title: res.data.message,
          icon: 'none'
        });
      } else {
        uni.showToast({
          title: res.data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.u-popup-slot {
  width: 200px;
  height: 150px;
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  view {
    margin: 20rpx 0;
  }
}

.content {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #ccc;

  .content-header {
    height: 300rpx;
    background-color: #02a7f0;

    text-align: center;
    font-size: 36rpx;
    font-weight: 700;
    color: #ffffff;
  }

  .content-form {
    position: fixed;
    top: 5%;
    left: 10%;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    width: 80%;
    border-radius: 30rpx;
    background-color: #ffffff;

    input {
      height: 60rpx;
      font-size: 20rpx;
    }

    .u-form-item__body__left__content__label.data-v-5e7216f1 {
      font-size: 24rpx;
    }

    .u-form-item__body.data-v-5e7216f1 {
      padding: 5px;
    }
  }
}

.mymessage {
  z-index: 1000;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500rpx;
  height: 300rpx;
  border-radius: 30rpx;
  background-color: #ffffff;
  overflow: hidden;
  box-shadow: 0 0 5rpx #ccc;
  .showheader {
    height: 80rpx;
    background-color: #e8e8e8;
    font-size: 38rpx;
    text-align: center;
    line-height: 80rpx;
  }
  .showtext {
    margin-top: 20rpx;
    height: 80rpx;
    background-color: #ffffff;
    text-align: center;
    font-size: 24rpx;
    line-height: 80rpx;
  }
  .showbtns {
    margin-top: 20rpx;
    height: 80rpx;
    display: flex;
    padding: 0 20rpx;
    background-color: #ffffff;
    justify-content: space-around;
    .btns {
      width: 150rpx;
      height: 50rpx;
      border-radius: 5rpx;
      text-align: center;
      line-height: 50rpx;
      color: #ffffff;
    }
  }
}
</style>
